<template>
    <view class="cn-account">
        <custom :tap_back="()=>{$store.commit('userLaunchTo', '/page/index')}"><template #backText>登录</template></custom>
        <view class="flex justify-center">
            <image class="cn-logo" src="/static/image/logo/122x122.png"></image>
        </view>
        <view class="padding-lr-30">
            <nFormItem icon='cuIcon-people' placeholder="手机号 / 账号" :data.sync="data" field="phone"></nFormItem>
            <nFormItem icon='cuIcon-command' placeholder="登录密码" :data.sync="data" field="password_md5" :password="true"></nFormItem>
            <nFormButton title="登录" :tap="login"></nFormButton>
            <view class="margin-lr-30 flex justify-between text-999">
                <view @tap="$store.commit('userNavTo', '/account/passwordForgot')">忘记密码</view>
                <view @tap="$store.commit('userNavTo', '/account/register')">诚邀注册</view>
            </view>
            <view class="cu-form-group margin-top-30 text-red" v-if="envIndex != -1">
                <view class="title">选择环境</view>
                <picker @change="envChange" :value.sync="envIndex" :range="envRange">
                    <view class="picker">
                        {{envRange[envIndex]}}
                    </view>
                </picker>
            </view>
        </view>
        <a v-if="app_type == 0" :href="'https://zy-jdc.oss-cn-hangzhou.aliyuncs.com/apk/joa-'+version_no+'.apk'" target='_blank'>
            <foot title="下载安卓版"></foot>
        </a>
    </view>
</template>

<script>
    export default {
        data() {
            let envIndex = -1;
            let envRange = [];
            if (this.$app.env != 'production') {
                envRange = this.$app.envs;
                envRange.forEach((node_env, index) => {
                    if (node_env == this.$app.env) {
                        envIndex = index
                    }
                })
            }
            return {
                data: {
                    formtag: {},
                    form: {
                        phone: '',
                        password_md5: ''
                    },
                },
                app_type: this.$app.app_type,
                version_no: this.$app.version_no,
                envIndex: envIndex,
                envRange: envRange
            }
        },
        methods: {
            envChange(e) {
                this.envIndex = e.target.value;
                this.$app.changeEnv(this.envRange[e.target.value]);
            },
            login() {
                this.$api.post(this, {
                    loading: '登录中',
                    url: '/account/login/phonePassword',
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result)) return;
                        this.$store.commit('userLogin', Result.result);
                    }
                });
            },
        }
    }
</script>

<style lang="less">
    @import "./account.less";
</style>
